#include "stream/impl/rocksdb/Data.h"
#include <iostream>
#include "stream/impl/rocksdb/Rocksdb.h"

namespace stream {
namespace impl {
namespace rocksdb {

Data::Data(const std::string & path)
: _path(path)
, _db( new Rocksdb(path)) {
}

Data::~Data(){
}

int Data::append(const std::string & key, const std::string & value) {
	auto s = _db->put(key, value);
	if(s.ok()){
		return (int)value.size();
	}
	return -1;
}

bool Data::get(const std::string & key, std::string & value) {
	auto s = _db->get(key, value);
	if(s.ok()){
		return true;
	}
	else if(s.IsNotFound()){
		return false;
	}
	else
		return false;
}

bool Data::size(size_t & sz) const {
	return _db->size(sz);
}
void Data::clear() {
	size_t sz = 0;
	if(this->size(sz)){
		for(size_t i = 0; i < sz; ++i){
			_db->del(std::to_string(i));
			std::cout << "delete key:" << i << std::endl;
		}
	}
}

bool Data::_open(){
	return _db->createIfMissing();
}

void Data::_close() {
	_db->close();
}

stream::impl::rocksdb::Data *  makeData(const std::string & path){
	return new ::stream::impl::rocksdb::Data(path);
}

}//end namespace rocksdb
}//end namespace impl
}//end namespace stream
